<template>
	<view class="container flex f-column f-align">
		<image src="../static/img/ps-tubiao@2x.png" style="width: 120rpx;height: 120rpx;"></image>
		<text class="font36 blue mt30">智能洗衣</text>
		<view class="login-box">
			<view class="flex f-align input-box">
				账号 <input type="text" v-model="formData.account" maxlength="20" class="input-item" />
			</view>
			<view class="flex f-align input-box">
				密码 <input type="password" v-model="formData.password" maxlength="20" class="input-item" />
			</view>
		</view>
		<view class="flex f-align xy-box" @click="forgetHandle">
			<icon type="success" size="16" :color="(isForget && forgetItem.title == title) ? '' : '#C0C4CC'"></icon>
			<view class="font24 gray999 ml10">
				记住密码
			</view>
		</view>
		<view class="login-btn font32 white f-center" @click="loginHandle">登录</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					account: '',
					password: ''
				},
				type: '',
				isForget: uni.getStorageSync('isForget') || true,
				forgetItem: { title: '' },
				title: uni.getStorageSync('pageTitle') || '工厂入口'
			}
		},
		onLoad() {
			if (this.isForget) {
				if (this.title == '校园入口') {
					this.forgetItem = uni.getStorageSync('forgetItem_xy') || { title: this.title }
				} else if (this.title == '工厂入口') {
					this.forgetItem = uni.getStorageSync('forgetItem_gc') || { title: this.title }
				} else if (this.title == '配送入口') {
					this.forgetItem = uni.getStorageSync('forgetItem_ps') || { title: this.title }
				}
				
				this.formData.account = this.forgetItem.account
				this.formData.password = this.forgetItem.password
			}
		},
		onReady() {
			if (this.title == '校园入口') {
				this.type = '校园管理员'
			} else if (this.title == '配送入口') {
				this.type = '配送员'
			} else {
				this.type = '工厂人员'
			}
			uni.setNavigationBarTitle({
				title: this.title
			})
		},
		methods: {
			forgetHandle() {
				if (this.forgetItem.title == this.title) {
					this.isForget = !this.isForget
				}
				
				this.forgetItem.title = this.title
			},
			loginHandle() {
				if (!this.formData.account) {
					this.$api.msg('请输入账号')
					return
				}
				if (!this.formData.password) {
					this.$api.msg('请输入密码')
					return
				}
				uni.showLoading({
					title: '登录中...'
				})
				
				this.$http.ajax({
					url: '/Admin/login',
					params: Object.assign(this.formData, { type: this.type }),
				}).then((res) => {
					uni.hideLoading()
					if (res.code == 200) {
						if (res.admin.merchantId == uni.getStorageSync('merchantId')) {
							uni.setStorageSync('isForget', this.isForget)
							if (this.isForget) {
								if (this.title == '校园入口') {
									uni.setStorageSync('forgetItem_xy', { title: this.title, account: this.formData.account, password: this.formData.password })
								} else if (this.title == '工厂入口') {
									uni.setStorageSync('forgetItem_gc', { title: this.title, account: this.formData.account, password: this.formData.password })
								} else if (this.title == '配送入口') {
									uni.setStorageSync('forgetItem_ps', { title: this.title, account: this.formData.account, password: this.formData.password })
								}
							} else {
								if (this.title == '校园入口') {
									uni.setStorageSync('forgetItem_xy', '')
								} else if (this.title == '工厂入口') {
									uni.setStorageSync('forgetItem_gc', '')
								} else if (this.title == '配送入口') {
									uni.setStorageSync('forgetItem_ps', '')
								}
							}
							uni.setStorageSync('adminId', res.admin.id)
							uni.setStorageSync('selfGzbh', res.admin.gzbh)
							this.$api.toPage('./roleEnter')
						} else {
							this.$api.msg('平台与账号不属于同一个商家')
						}
					} else {
						this.$api.msg(res.msg)
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		padding-top: 80rpx;

		.login-box {
			width: 750rpx;
			border-top: 2rpx solid #cccccc;
			margin-top: 174rpx;

			.input-box {
				padding: 28rpx 92rpx;
				font-size: 32rpx;
				line-height: 32rpx;
				color: #A6A6A6;
				border-bottom: 2rpx solid #cccccc;
			}
			
			.input-item {
				margin-left: 20rpx;
				color: #000000;
			}
		}

		.login-btn {
			width: 600rpx;
			height: 80rpx;
			background: #4082FD;
			border-radius: 40rpx;
			margin-top: 50rpx;
		}

		.xy-box {
			width: 100%;
			padding: 0 60rpx;
			margin-top: 50rpx;
			justify-content: flex-end;
			text {
				text-decoration: underline;
			}
		}
	}
</style>
